package com.moon.back.config;

import com.moon.back.controller.intercepter.GlobalIntercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @author 16905
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

        @Override
    public void addCorsMappings(CorsRegistry registry) {
            //4.去掉方法体里原有的内容,编写自己的请求策略
            registry.addMapping("/**")
                    // /** 表示在后端允许匹配客户端发过来的任意请求
                    .allowedHeaders("*")
                    //请求带任意头都可以
                    .allowedMethods("*")
                    //任意请求方式都可以 get/post/put...
                    .allowedOriginPatterns("*")
                    //任意域都可以(任意请求地址或端口号)
                    .allowCredentials(true)
                    //请求可以携带会话相关信息(cookie/session)
                    .maxAge(3600);
            //同一请求一小时内不再检测 直接放行
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new GlobalIntercepter()).addPathPatterns("/**")
                .excludePathPatterns("/user/login","/user/register","/file/downloadFile");
    }

    /**
     * 注册资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}